package com.ruoyi.project.system.login.controller;


import com.ruoyi.common.utils.JWTUtils;
import com.ruoyi.framework.shiro.service.PasswordService;
import com.ruoyi.framework.web.domain.Result;
import com.ruoyi.framework.web.domain.ResultToken;
import com.ruoyi.project.system.login.domain.MiniSysUser;
import com.ruoyi.project.system.login.service.IMiniSysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class BaseUserController {

    @Autowired
    private IMiniSysUserService miniSysUserService;

    @Autowired
    private PasswordService passwordService;

    /**
     * 用户登录
     *
     * @param miniSysUser BaseUser实体类
     * @return 如果能查询到则返回对象，查询不到则无数据
     */
    @PostMapping("/login")
    public ResultToken loginTest(@RequestBody MiniSysUser miniSysUser){
        MiniSysUser miniSysUserSalt = miniSysUserService.selectMiniSysUserByLoginName(miniSysUser.getLoginName());
        if(miniSysUserSalt==null){
            return ResultToken.error("用户名或密码错误",202);
        }
        String s = passwordService.encryptPassword(miniSysUser.getLoginName(), miniSysUser.getPassword(), miniSysUserSalt.getSalt());
        MiniSysUser miniSysUserInfo = miniSysUserService.selectMiniSysUserByUnameAndPwd(miniSysUser.getLoginName(), s);
        if(miniSysUserInfo!=null){
            String token = JWTUtils.genToken(miniSysUserInfo.getLoginName(),miniSysUserInfo.getPassword());
            return ResultToken.successToken(miniSysUserInfo,token);
        }else {
            return ResultToken.error("用户名或密码错误",202);
        }


    }

}
